package com.controller.provider;

import com.javabean.Provider;
import com.service.bill.BillService;
import com.service.provider.ProviderService;
import com.service.role.RoleService;
import com.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class ProviderController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private BillService billService;
    @Autowired
    private ProviderService providerService;


    @RequestMapping("/provider.do")
    public String showProviderList(HttpServletRequest request, Model model) {

        String queryProCode = request.getParameter("queryProCode");
        String queryProName = request.getParameter("queryProName");
        if (queryProCode == null) {
            queryProCode = "";
        }

        if (queryProName == null) {
            queryProName = "";
        }

        List<Provider> providerList = providerService.getProviderList(queryProName, queryProCode);
        for (Provider provider : providerList) {
            model.addAttribute("provider", provider);
        }
        model.addAttribute("providerList", providerList);
        model.addAttribute("queryProCode", queryProCode);
        model.addAttribute("queryProName", queryProName);

        return "providerlist";
    }

    @RequestMapping("/providerView")
    public String providerView(Model model, HttpServletRequest request) {
        String proCode = request.getParameter("proCode");
        String proName = request.getParameter("proName");
        String proContact = request.getParameter("proContact");
        String proPhone = request.getParameter("proPhone");
        String proFax = request.getParameter("proFax");
        String proDesc = request.getParameter("proDesc");
        model.addAttribute("proCode", proCode);
        model.addAttribute("proName", proName);
        model.addAttribute("proContact", proContact);
        model.addAttribute("proPhone", proPhone);
        model.addAttribute("proFax", proFax);
        model.addAttribute("proDesc", proDesc);
        return "providerview";
    }


    @RequestMapping("/delprovider")
    public void delprovider(Model model, HttpServletRequest request, HttpServletResponse response) {
        int proid = Integer.parseInt(request.getParameter("proid"));
        providerService.deleteProviderById(proid);

        try {
            response.sendRedirect(request.getContextPath() + "/provider.do");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
